package www.sharetime.weshare.activity;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.BindView;
import butterknife.OnClick;
import www.sharetime.weshare.R;
import www.sharetime.weshare.base.BaseActivity;
import www.sharetime.weshare.event.AddTagEvent;
import www.sharetime.weshare.event.PictureSelectCompleteEvent;
import www.sharetime.weshare.event.ShareContentEvent;
import www.sharetime.weshare.pictureselector.view.PicturesPreviewRecyclerView;
import www.sharetime.weshare.tag.model.TagGroupModel;
import www.sharetime.weshare.utils.L;
import www.sharetime.weshare.utils.UIUtils;

/**
 * File Description  : 分享发布页面
 *
 * @author : zhanggeng
 * @version : v1.0
 *          **************修订历史*************
 * @email : zhanggengdyx@gmail.com
 * @date : 2017/9/17 13:38
 */

public class SharePublishActivity extends BaseActivity {
    @BindView(R.id.img_share_close_action)
    ImageView imgShareCloseAction;
    @BindView(R.id.img_share_publish_action)
    ImageView imgSharePublishAction;
    @BindView(R.id.rl_publish_title)
    RelativeLayout rlPublishTitle;
    @BindView(R.id.tv_ge)
    TextView tvGe;
    @BindView(R.id.edt_share_title)
    TextView edtShareTitle;
    @BindView(R.id.tv_ge1)
    TextView tvGe1;
    @BindView(R.id.edt_share_content)
    TextView edtShareContent;
    @BindView(R.id.img_add_pic)
    ImageView imgAddPic;
    @BindView(R.id.recycler_images)
    PicturesPreviewRecyclerView recyclerImages;
    @BindView(R.id.tv_select_address)
    TextView tvSelectAddress;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_share_publish;
    }

    @Override
    public void initView(Bundle savedInstanceState) {
        super.initView(savedInstanceState);

    }

    @Override
    public void initData() {
        super.initData();
    }

    /**
     * 给fragment 添加tag
     *
     * @param tagGroupModel
     */
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onTagCreate(TagGroupModel tagGroupModel) {
        if (tagGroupModel != null) {
            //TODO： 拿到图片，标签、坐标的对应关系上传给服务器
        }
    }

    /**
     * 添加图片动作的隐藏显示按钮
     *
     * @param event
     */
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onPicSelecComplete(PictureSelectCompleteEvent event) {
        if (event != null) {
            L.et("length", recyclerImages.getPaths().length + "=======");
            if (recyclerImages.getPaths().length > 1) {
                imgAddPic.setVisibility(View.GONE);
            } else {
                imgAddPic.setVisibility(View.VISIBLE);
                recyclerImages.setVisibility(View.GONE);
            }
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onShareContent(ShareContentEvent shareContentEvent) {
        if (shareContentEvent != null && shareContentEvent.getTitle() != null && shareContentEvent.getContent() != null) {
            edtShareTitle.setText(shareContentEvent.getTitle());
            edtShareContent.setText(shareContentEvent.getContent());
        }
    }

    /**
     * 选择地点
     *
     * @param addTagEvent
     */
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onSelectAddress(AddTagEvent addTagEvent) {
        if (addTagEvent != null) {
            if (addTagEvent.getFrom().equals(CitySearchActivity.class.getSimpleName())) {
                tvSelectAddress.setText(addTagEvent.getValue());
            }
        }
    }

    @OnClick({
            R.id.img_share_close_action,
            R.id.img_share_publish_action,
            R.id.rl_publish_title,
            R.id.edt_share_title,
            R.id.img_add_pic,
            R.id.edt_share_content,
            R.id.tv_select_address
    })
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.img_share_close_action:
                //TODO: 弹出对话框，提示是否要退出
                areYouSureClose();
                break;
            case R.id.img_share_publish_action:
                //TODO: 真实发布动作
                publicShareAction();
                break;
            case R.id.rl_publish_title:
                break;
            case R.id.edt_share_title:
            case R.id.edt_share_content:
                jumpToEditContentPage();
                break;
            case R.id.img_add_pic:
                recyclerImages.setVisibility(View.VISIBLE);
                recyclerImages.onLoadAllPictures();
                break;
            case R.id.tv_select_address:
                UIUtils.startActivity(SharePublishActivity.this, CitySearchActivity.class);
                break;
        }
    }

    private void jumpToEditContentPage() {
        UIUtils.startActivity(SharePublishActivity.this, EditShareActivity.class);
    }

    private void publicShareAction() {
    }

    private void areYouSureClose() {
        if (!edtShareTitle.getText().toString().trim().equals("") || !edtShareContent.getText().toString().trim().equals("") || recyclerImages.getPaths().length > 0) {
            popRemindDialog();
        } else {
            UIUtils.closeActivity(this);
        }
    }

    private void popRemindDialog() {
        new AlertDialog.Builder(SharePublishActivity.this)
                .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                })
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                        UIUtils.closeActivity(SharePublishActivity.this);
                    }
                }).setTitle("退出编辑").setMessage("你确定要退出编辑吗？")
                .create().show();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        areYouSureClose();
        return super.onKeyDown(keyCode, event);
    }
}
